package com.jeesite.modules.ic.service;

import java.util.ArrayList;
import java.util.List;

import com.jeesite.modules.ic.channel.CardDataService;
import com.jeesite.modules.ic.entity.IcPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.ic.entity.IcChannel;
import com.jeesite.modules.ic.dao.IcChannelDao;

/**
 * 运营商Service
 * @author 李寒冰
 * @version 2024-07-15
 */
@Service
public class IcChannelService extends CrudService<IcChannelDao, IcChannel> {

	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private IcPackageService icPackageService;
	
	/**
	 * 获取单条数据
	 * @param icChannel
	 * @return
	 */
	@Override
	public IcChannel get(IcChannel icChannel) {
		return super.get(icChannel);
	}
	
	/**
	 * 查询分页数据
	 * @param icChannel 查询条件
	 * @param icChannel page 分页对象
	 * @return
	 */
	@Override
	public Page<IcChannel> findPage(IcChannel icChannel) {
		return super.findPage(icChannel);
	}
	
	/**
	 * 查询列表数据
	 * @param icChannel
	 * @return
	 */
	@Override
	public List<IcChannel> findList(IcChannel icChannel) {
		return super.findList(icChannel);
	}

	/**
	 * 同步套餐
	 * @param icChannel
	 */
	public void getPackage(IcChannel icChannel) {
		String id = icChannel.getId();
		CardDataService bean = applicationContext.getBean("card_data_" + id, CardDataService.class);
		List<IcPackage> aPackage = bean.getPackage();
		String[] packageList = new String[aPackage.size()];
		for (int i = 0; i < aPackage.size(); i++) {
			IcPackage icPackage = aPackage.get(i);
			packageList[i]= icPackage.getChannelPackageId();

		}
		IcPackage icPackage_search_notin = new IcPackage();
		icPackage_search_notin.notInPackage(packageList);
		icPackage_search_notin.setChannalId(id);
		List<IcPackage> listNoIn = icPackageService.findList(icPackage_search_notin);
		for (int i = 0; i < listNoIn.size(); i++) {
			icPackageService.delete(listNoIn.get(i));
		}

		for (int i = 0; i < aPackage.size(); i++) {
			IcPackage icPackage = aPackage.get(i);
			String channelPackageId = icPackage.getChannelPackageId();
			IcPackage icPackage_search = new IcPackage();
			icPackage_search.setChannelPackageId(channelPackageId);
			icPackage_search.setChannalId(id);
			IcPackage one = icPackageService.getOne(icPackage_search);
			if(one!=null){
				icPackage.setId(one.getId());
				icChannel.setIsNewRecord(false);
			}else{
				icChannel.setIsNewRecord(true);
			}
			icPackageService.save(icPackage);

		}


	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param icChannel
	 */
	@Override
	@Transactional
	public void save(IcChannel icChannel) {
		super.save(icChannel);
	}
	
	/**
	 * 更新状态
	 * @param icChannel
	 */
	@Override
	@Transactional
	public void updateStatus(IcChannel icChannel) {
		super.updateStatus(icChannel);
	}
	
	/**
	 * 删除数据
	 * @param icChannel
	 */
	@Override
	@Transactional
	public void delete(IcChannel icChannel) {
		super.delete(icChannel);
	}
	
}